package com.team6.stickynote.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.PopupMenu;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.team6.stickynote.App;
import com.team6.stickynote.R;
import com.team6.stickynote.adapter.StickyNoteAdapter;
import com.team6.stickynote.adapter.StickyNoteDiffCallback;
import com.team6.stickynote.base.BaseActivity;
import com.team6.stickynote.databinding.ActivityMainBinding;
import com.team6.stickynote.util.ToastUtil;

public class MainActivity extends BaseActivity {

    private ActivityMainBinding binding;

    private MainViewModel mainViewModel;

    private StickyNoteAdapter stickyNoteAdapter;

    @Override
    protected void initBinding() {
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 在 onCreate 方法中初始化 ViewModel
        mainViewModel = new ViewModelProvider(this).get(MainViewModel.class);

        // Sticky Note 适配器
        stickyNoteAdapter = new StickyNoteAdapter(new StickyNoteDiffCallback());
        stickyNoteAdapter.setOnItemClickListener(id -> {
            Intent intent = new Intent(MainActivity.this, EditActivity.class);
            intent.putExtra(EditActivity.EXTRA_STICKY_NOTE_BEAN_ID, id);
            startActivity(intent);
        });
        stickyNoteAdapter.setOnItemLongClickListener((view, id) -> {
            //长按事件
            //为该View设置弹出框
            PopupMenu popupMenu = new PopupMenu(MainActivity.this, view);
            popupMenu.getMenuInflater().inflate(R.menu.delete_menu, popupMenu.getMenu()); // 设置样式
            popupMenu.show(); // 显示
            //设置点击事件
            popupMenu.setOnMenuItemClickListener(item -> {
                switch (item.getItemId()){
                    case R.id.delete_it:
                        // 删除该item
                        ToastUtil.toast("删除成功！");
                        App.database.deleteOneData(id);
                        // 更新显示数据
                        mainViewModel.updateStickyNoteBeans();
                        break;
                }
                return true;
            });
        });

        // rv 设置适配器
        binding.rvStickyNote.setAdapter(stickyNoteAdapter);

        showList(true);//默认布局
        // 获取组件，设置点击事件
        binding.ivLayout.setOnClickListener(this::setMenu);
        binding.ivSearch.setOnClickListener(view -> {
            Intent intent = new Intent(MainActivity.this, SearchActivity.class);
            startActivity(intent);
        });

        binding.floatingActionButton.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, AddActivity.class);
            startActivity(intent);
        });

        // 数据监听
        mainViewModel.stickyNoteBeans.observe(this, stickyNoteBeans -> {
            stickyNoteAdapter.submitList(stickyNoteBeans);
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 请求更新数据
        // mainViewModel.updateStickyNoteBeans();

        showList(true); // 默认布局
        mainViewModel.updateStickyNoteBeans();
    }

    //用于设置PopupMenu样式
    @SuppressLint("NonConstantResourceId")
    void setMenu(View view) {
        //获取menu
        PopupMenu menu = new PopupMenu(MainActivity.this, view);//view为menu所依附的组件
        //加载选项菜单栏资源
        menu.getMenuInflater().inflate(R.menu.main_menu, menu.getMenu());
        //显示menu
        menu.show();
        //为PopupMenu设置点击监听器
        menu.setOnMenuItemClickListener(item -> {
            int itemId = item.getItemId();//获取被点击的item的ID
            switch (itemId) {
                //实现ListView效果
                case R.id.item1_vertival:
                    showList(true);
                    break;
                case R.id.item1_horizontal:
                    showList(false);
                    break;
                //实现GridView效果
                case R.id.item2_vertival:
                    showGrid(true);
                    break;
                case R.id.item2_horizontal:
                    showGrid(false);
                    break;
                //实现瀑布流效果
                case R.id.item3_vertival:
                    showStagger(true);
                    break;
                case R.id.item3_horizontal:
                    showStagger(false);
                    break;
                default:
                    break;
            }

            return true;
        });
    }

    //用于实现ListView布局
    void showList(boolean isVertical) {
        //创建布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        //设置方向定位
        if (isVertical) {
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        } else {
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        }
        //设置布局管理器
        binding.rvStickyNote.setLayoutManager(linearLayoutManager);
    }

    // 用于实现 Grid 布局
    void showGrid(boolean isvertival) {
        //创建布局管理器
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        //设置方向定位
        if (isvertival) {
            gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        } else {
            gridLayoutManager.setOrientation(GridLayoutManager.HORIZONTAL);
        }
        //设置布局管理器
        binding.rvStickyNote.setLayoutManager(gridLayoutManager);
    }

    // 用于实现瀑布流布局
    private void showStagger(boolean isVertical) {
        //创建布局管理器
        int ORIENTATION = isVertical ? StaggeredGridLayoutManager.VERTICAL : StaggeredGridLayoutManager.HORIZONTAL;
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, ORIENTATION);
        //设置布局管理器
        binding.rvStickyNote.setLayoutManager(staggeredGridLayoutManager);
    }

}